﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using Quartz.Impl;
using System.Collections.Specialized;
using Quartz.Core;
using System.Threading;

namespace EmailSchedule
{
    class MainJob:IJob
    {
        public virtual void Execute(IJobExecutionContext context)
        {
            try
            {
                Helper.InitConfig();
                //  var properties = new NameValueCollection { { "quartz.threadPool.threadCount", "30" } };
                if (Helper.IsFlag)
                {
                    ISchedulerFactory _scheduleFact = new StdSchedulerFactory();
                    IScheduler _sched = _scheduleFact.GetScheduler();
                    _sched.Start();
                    //Job Get Email
                    for (int i = 1; i <= 3; i++)
                    {
                        Thread.Sleep(4000);
                        IJobDetail getEmailCustomerJob = JobBuilder.Create<GetEmailCustomerJob>().WithIdentity("getEmailCustomerJob" + i.ToString(), "getEmailCustomerGroup" + i.ToString()).StoreDurably(true).RequestRecovery(false).Build();
                        ITrigger getEmailCustomerTrigger = TriggerBuilder.Create().WithIdentity("getEmailCustomerTrigger" + i.ToString(), "getEmailCustomerTrigger" + i.ToString()).StartNow().WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever()).Build();
                        _sched.ScheduleJob(getEmailCustomerJob, getEmailCustomerTrigger);
                    }
                    //IJobDetail getEmailCustomerJob = JobBuilder.Create<GetEmailCustomerJob>().WithIdentity("getEmailCustomerJob", "getEmailCustomerGroup").StoreDurably(true).RequestRecovery(false).Build();
                    //ITrigger getEmailCustomerTrigger = TriggerBuilder.Create().WithIdentity("getEmailCustomerTrigger", "getEmailCustomerTrigger" ).StartNow().WithSimpleSchedule(x => x..WithIntervalInSeconds(3).WithRepeatCount(100)).Build();
                    //_sched.ScheduleJob(getEmailCustomerJob, getEmailCustomerTrigger);
                    //Job send Email

                    IJobDetail sendEmailQueueJob = JobBuilder.Create<sendEmailToQueue>().WithIdentity("sendEmailQueueJob", "sendEmailQueueGroup").StoreDurably(true).RequestRecovery(false).Build();
                    ITrigger sendEmailQueueTrigger = TriggerBuilder.Create().WithIdentity("sendEmailQueueTrigger", "sendEmailQueueTrigger").StartNow().WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever()).Build();

                    IJobDetail sendEmailCustomerJob = JobBuilder.Create<sendEmailCustomerJob>().WithIdentity("sendEmailCustomerJob", "sendEmailCustomerGroup").StoreDurably(true).RequestRecovery(false).Build();
                    ITrigger sendEmailCustomerTrigger = TriggerBuilder.Create().WithIdentity("sendEmailCustomerTrigger", "sendEmailCustomerTrigger").StartNow().WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever()).Build();

                    IJobDetail respondEmailFromQueueJob = JobBuilder.Create<respondEmailFromQueue>().WithIdentity("respondEmailFromQueueJob", "respondEmailFromQueuerGroup").StoreDurably(true).RequestRecovery(false).Build();
                    ITrigger respondEmailFromQueueTrigger = TriggerBuilder.Create().WithIdentity("respondEmailFromQueueTrigger", "respondEmailFromQueueTrigger").StartNow().WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever()).Build();

                    _sched.ScheduleJob(sendEmailQueueJob, sendEmailQueueTrigger);
                    _sched.ScheduleJob(sendEmailCustomerJob, sendEmailCustomerTrigger);
                    _sched.ScheduleJob(respondEmailFromQueueJob, respondEmailFromQueueTrigger);
                }
            }
            catch (Exception ex)
            {
                Helper.LogToFile(LogFileType.EXCEPTION,"Error : "+ ex.Message, "Startup");
            }
        }
    }
}
